package com.bwie.controller;

import com.bwie.pojo.Goods;
import com.bwie.pojo.ShoppingCart;
import com.bwie.pojo.User;
import com.bwie.result.Result;
import com.bwie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;


    @PostMapping("/addShoppingCart")
    public Result addShoppingCart(@RequestBody ShoppingCart shoppingCart){
        shoppingCartService.addShoppingCart(shoppingCart);
        Result result = Result.success();
        return result;
    }

    @PostMapping("/shoppingCartList")
    public Result<List<ShoppingCart>> shoppingCartList(){
        List<ShoppingCart> list=shoppingCartService.shoppingCartList();
        Result<List<ShoppingCart>> result = Result.success(list);
        return result;
    }


    @PostMapping("/deleteGoods/{id}")
    public Result deleteGoods(@PathVariable("id") Integer id){
        shoppingCartService.deleteGoods(id);
        Result result = Result.success();
        return result;
    }

    @GetMapping("/findCountAndPrice")
    public Result findCountAndPrice(){
        ShoppingCart shoppingCart=shoppingCartService.findCountAndPrice();
        Result<ShoppingCart> result = Result.success(shoppingCart);
        return result;
    }

    @PostMapping("/buyShopping")
    public Result buyShopping(@RequestBody List<ShoppingCart> shoppingCart){
        shoppingCartService.buyShopping(shoppingCart);
        Result result = Result.success();
        return result;
    }

    @GetMapping("/findUserById/{id}")
    public Result<User> findUserById(@PathVariable("id") Integer id){
        User user=shoppingCartService.findUserById(id);
        Result<User> result = Result.success(user);
        return result;
    }

    /**
     * 批量删除购物车
     */
    @PostMapping("/delAll")
    public Result delAll(@RequestBody List<Integer> ids){
        shoppingCartService.delAll(ids);
        Result result = Result.success();
        return result;
    }

}
